package org.bitmap.mm.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

final class BitmapLove {
	
	private static final String TAG = "BitmapLove";

	public static synchronized Bitmap decodeSampledBitmapFromResources(
			Context ctx, int resId, int reqWidth) {
		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(ctx.getResources(), resId, options);

		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		options.inInputShareable = true;
		options.inPurgeable = true;

		final BitmapFactory.Options options2 = new BitmapFactory.Options();
		options2.inSampleSize = options.inSampleSize;
		options2.inJustDecodeBounds = false;
		float mreq = ((float) reqWidth / (float) options.outWidth);
		float newBitmapHeight = mreq * options.outHeight;
		options2.outWidth = reqWidth;
		options2.outHeight = (int) newBitmapHeight;
		return BitmapFactory
				.decodeResource(ctx.getResources(), resId, options2);
	}

	public static synchronized BitmapFactory.Options getBitmapWidthAndHeigth(
			String filename) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(filename, options);
		return options;
	}

	public static synchronized Bitmap decodeSampledBitmapFromFile(
			String filename, int reqWidth, Bitmap.Config bconfig) {
		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(filename, options);
		System.out.println(TAG + "-width:" + options.outWidth + " -heigth:"
				+ options.outHeight);
		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth);
		System.out
				.println("BitmapManager-inSampleSize:" + options.inSampleSize);
		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		options.inPurgeable = true;
		if (bconfig != null) {
			options.inPreferredConfig = bconfig;
		} else {
			options.inPreferredConfig = Bitmap.Config.RGB_565;
		}
		if (options.inSampleSize <= 1) {
			options.inSampleSize = 3;
		} else {
			options.inSampleSize = (options.inSampleSize + 1);
		}
		Bitmap bm = null;
		try {
			bm = BitmapFactory.decodeFile(filename, options);
		} catch (OutOfMemoryError err) {

		}
		return bm;
	}

	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		float mreq = ((float) reqWidth / (float) width);
		float newBitmapHeight = mreq * height;

		int rewHeigth = ((int) newBitmapHeight * height);
		if (width > reqWidth) {
			if (width > height) {
				inSampleSize = Math.round((float) height / (float) rewHeigth);
			} else {
				inSampleSize = Math.round((float) width / (float) reqWidth);
			}

			// This offers some additional logic in case the image has a strange
			// aspect ratio. For example, a panorama may have a much larger
			// width than height. In these cases the total pixels might still
			// end up being too large to fit comfortably in memory, so we should
			// be more aggressive with sample down the image (=larger
			// inSampleSize).

//			final float totalPixels = width * height;
//
//			// Anything more than 2x the requested pixels we'll sample down
//			// further.
//			final float totalReqPixelsCap = reqWidth * rewHeigth * 2;
//
//			while (totalPixels / (inSampleSize * inSampleSize) >Math.abs(totalReqPixelsCap) ) {
//				inSampleSize++;
//			}
		}
		return inSampleSize;
	}
}
